package com.starblink.android.basic.aroute

import android.content.Context
import com.starblink.android.basic.ext.commContext
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.navigationTo
import com.starblink.basic.route.RoutePage
import com.starblink.basic.storage.SkStorage
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.rocketreserver.fragment.ProductF

object CommonRoute {
    /**
     * 打开商品外链 尽量传详细数据
     * affInfoLink 必传 ProductD.AffInfo中link参数 用于affInfoLink作用
     *
     * !!!!注意，跳转外链时，需要在 SpmTrackHandler增加 reportList埋点位，用作上报统计----重要
     */
    fun startProductLink(
        activity: Context,
        webUrl: String?,
        id: String,
        productId: String,
        standardSpuIds: String,
        collect: Boolean,
        type: Int,
        storeId: String,
        storeName: String,
        storeLogo: String?,
        storeFollow: Boolean,
        affInfoLink: String?,
        traceId: String = "",
    ) {
        if (webUrl?.isNotEmpty() == true) {
            navigationTo(RoutePage.Product.PAGE_LINK) {
                withString(RoutePage.Product.TRACE_ID, traceId)
                withString("id", id)
                withString(RoutePage.Product.PRODUCT_ID, productId)
                withString(RoutePage.Product.STANDARD_SPU_ID, standardSpuIds)
                withBoolean(RoutePage.Product.COLLECT, collect)
                withInt(RoutePage.Product.TYPE, type)
                if (affInfoLink?.isNotEmpty() == true) {
                    withString(RoutePage.Store.URL, affInfoLink)
                } else {
                    withString(RoutePage.Store.URL, webUrl)
                }
                withString(RoutePage.Store.STORE_NAME, storeName)
                withString(RoutePage.Store.STORE_LOGO, storeLogo)
                withString(RoutePage.Store.STORE_ID, storeId)
                withBoolean(RoutePage.Store.SUB_OR_NOT, storeFollow)
            }
        }
    }

    /**
     * 打开商品详情（大卡）
     */
    fun startProductDetail(
        productId: String,
        standardSpuIds: String,
        product: ProductF?
    ) {
        if (SkStorage.getString("assign_product_card", "") == "b") {
            if (product != null) {
                startProductLink(
                    activity = commContext(),
                    webUrl = product.url,
                    affInfoLink = product.affInfo?.link,
                    id = product.id ?: "",
                    productId = product.productId ?: "",
                    standardSpuIds = product.standardSpuIds ?: "",
                    traceId = product.traceId ?: "",
                    type = product.type,
                    collect = product.collected == true,
                    storeId = product.merchantWeb?.id ?: "",
                    storeName = product.merchantWeb?.merchantName ?: "",
                    storeLogo = product.merchantWeb?.logo,
                    storeFollow = product.merchantWeb?.subOrNot == true,
                )
            }
        } else {
            navigationTo(RoutePage.Product.PAGE_DETAIL) {
                withString(RoutePage.Product.PRODUCT_ID, productId)
                withString(RoutePage.Product.STANDARD_SPU_ID, standardSpuIds)
                withObject(RoutePage.Product.PRODUCT, product)
                withObject(RoutePage.Product.TRACE_ID, product?.traceId)
            }
        }
    }

    /**
     * 打开商品外链（未获取到详细数据时用该方法）
     */
    fun startProductLinkByID(
        activity: Context,
        webUrl: String?,
        id: String,
        affInfoLink: String?,
    ) {
        activity.navigationTo(RoutePage.Product.PAGE_LINK) {
            withString("id", id)
            if (affInfoLink?.isNotEmpty() == true) {
                withString(RoutePage.Store.URL, affInfoLink)
            } else {
                withString(RoutePage.Store.URL, webUrl)
            }
        }
    }

    /**
     * 打开店铺详请
     */
    fun startStoreDetail(context: Context, storeId: String, productId: String? = null) {
        context.navigationTo(RoutePage.Store.PAGE_HOME) {
            withString(RoutePage.Store.ID, storeId)
            withString(RoutePage.Product.PRODUCT_ID, productId)
        }
    }

    /**
     * 打开店铺详请
     */
    fun startStoreLinkWithCode(
        storeId: String?,
        webUrl: String?, code: String, merchantDiscountInfo: MerchantWebVOF.MerchantDiscountInfo?
    ) {
//        var discountTip = ""
//        if (merchantDiscountInfo != null) {
//            discountTip =
//                merchantDiscountInfo!!.prefix + merchantDiscountInfo!!.discount + merchantDiscountInfo!!.suffix
//        }

        navigationTo(RoutePage.Store.PAGE_LINK) {
            withString(RoutePage.Store.STORE_ID, storeId)
            withString(RoutePage.Store.URL, webUrl)
            withString(RoutePage.Store.STORE_DISCOUNT_CODE, code)
            withString(RoutePage.Store.STORE_DISCOUNT, merchantDiscountInfo?.discount)
            withString("prefix", merchantDiscountInfo?.prefix)
            withString("suffix", merchantDiscountInfo?.suffix)
        }
    }

    /**
     * 去找相似商品
     */
    fun toFindSimilarGoods(imgUrl: String, productId: String, type: Int) {
        navigationTo(RoutePage.ImageSearch.PAGE_FIND_SIMILAR) {
            extraOf(
                RoutePage.ImageSearch.ARGS_IMG_URL to imgUrl,
                RoutePage.ARG.ID to productId,
                RoutePage.ARG.TYPE to type,
            )
        }
    }
}